-- NewTeamReadyAndSettingView
-- 队伍准备/队伍设置UI

local NewTeamMemberPanel = import(".NewTeamMemberPanel")
local NewTeamSettingPanel = import(".NewTeamSettingPanel")

local fileName = string.getModeName(...)
local ClassRef = class(fileName, app.mvc.ViewBase)

local bgContentSize = nil

ClassRef.VIEW_ROOT_RES = Res.NewTeamReadyAndSetting

function ClassRef:init()
	bgContentSize = self._ui["Panel/Panel_bg/bg01"]:getContentSize()

	self._ui["Panel/Button_close"]:addClickEventListener(function()
		executeFunc(self._onCloseView)
	end)

	-- 队伍成员准备节点
	self._teamMemberNode = self._ui["Panel/team"]
	local teamMemberPanel = self._teamMemberNode:getChildByName( "Panel" )
	teamMemberPanel:getChildByName( "Panel_tlt_team" ):setVisible( false )
	teamMemberPanel:getChildByName( "Panel_bom_1" ):setVisible( false )
	teamMemberPanel:getChildByName( "Panel_tlt_fb" ):setVisible( true )
	self._teamReadyTitle = teamMemberPanel:getChildByName( "Panel_tlt_fb" ):getChildByName( "tlt" )

	teamMemberPanel:getChildByName( "Panel_bom_2" ):setVisible( true )

	 -- 战备UI队伍战力
    self._panelBom2 = teamMemberPanel:getChildByName("Panel_bom_2")
	self._teamMemberCombatPower = self._panelBom2:getChildByName("combat_num")
    self._compatPowerImg = self._panelBom2:getChildByName("img")
    self._compatPowerTextTlt = self._panelBom2:getChildByName("combat_tlt")
	-- 成员列表
	self._teamMemberInfo = NewTeamMemberPanel.create( teamMemberPanel:getChildByName( "player" ) )
	self._teamMemberInfo:registerOnOperate(function(op, ...)
				executeFunc(self._onOperate, op, ...) -- 交由controller处理
			end)

	-- 队伍设置node
	self._teamSettingNode = self._ui["Panel/set"]
	self._teamSettingInfo = NewTeamSettingPanel.create( self._teamSettingNode )
	self._teamSettingInfo:registerOnOperate(function(op, ...)
				executeFunc(self._onOperate, op, ...) -- 交由controller处理
			end)

	self._tips = self._ui["Panel/tips"] -- 提示，秘境提示文本
	self._teamTarget = self._ui["Panel/Panel_top/target"] -- 队伍目标


	self._readyCancelBtn = self._ui["Panel/Panel_bom/Button_readyCancel"] -- 队伍准备取消按钮
	self._readyCancelBtn:addClickEventListener(function()
		if L("lua_code_text_258") == self._readySureBtn:getChildByName( "name" ):getString() then -- 队员？
			self._createTeamDialog = display.newConfirmDialog( string.format( L("lua_code_text_319") ),
				function() -- 确定
					executeFunc(self._onOperate, "TeamReadyAndSetting_replayReady", false )
					self._createTeamDialog = nil
				end,
				function() -- 取消
					self._createTeamDialog = nil
				end)
			UIHandler:addPopDialog( self._createTeamDialog )
		else -- 队长？
			executeFunc(self._onOperate, "TeamReadyAndSetting_cancelVote" )
		end
	end)

	self._readySureBtn = self._ui["Panel/Panel_bom/Button_readySure"] -- 队伍准备确定按钮
	self._readySureBtn:addClickEventListener(function()
		if L("lua_code_text_258") == self._readySureBtn:getChildByName( "name" ):getString() then -- 队员
			executeFunc(self._onOperate, "TeamReadyAndSetting_replayReady", true )
		else
			executeFunc(self._onOperate, "TeamReadyAndSetting_enterDungeons" ) -- 队长
		end
	end)

	self._settingSureBtn = self._ui["Panel/Panel_bom/Button_settingSure"] -- 别人队伍设置UI确定按钮
	self._settingSureBtn:setVisible( true )
	self._settingSureBtn:addClickEventListener(function()
		executeFunc(self._onOperate, "TeamReadyAndSetting_applyJoinTeam" )
		executeFunc(self._onCloseView)
	end)
end

function ClassRef:show()
	self:setVisible(true)
	self:openAction()
end

function ClassRef:removeCreateTeamDialog()
	if not cc.isDead(self._createTeamDialog) then
		self._createTeamDialog:removeFromParent()
		self._createTeamDialog = nil
	end
end

function ClassRef:showWhichPanelByIdx( idx )
	if 1 == idx  then -- 准备
		self._teamMemberNode:setVisible( true )
		self._readyCancelBtn:setVisible( true )
		self._readySureBtn:setVisible( true )

		self._teamSettingInfo:setVisible( false )
		self._settingSureBtn:setVisible( false )
		self._ui["Panel/Button_close"]:setVisible( false )
	elseif 2 == idx  then -- 设置
		self._teamMemberNode:setVisible( false )
		self._readyCancelBtn:setVisible( false )
		self._readySureBtn:setVisible( false )

		self._teamSettingInfo:setVisible( true )
		self._settingSureBtn:setVisible( true )
		self._ui["Panel/Button_close"]:setVisible( true )
	else
		print( " ----------------->>>>>>>> showWhichPanelByIdx:showWhichPanelByIdx 参数异常！" )
	end
end

-- 刷新战备倒计时可见性
function ClassRef:setTipsShown( b )
	b = b or false
	self._tips:setVisible( b )
	if b then
		self:_setCDTime()
	end
end
function ClassRef:_setCDTime()
	self._cdTime = tonumber( GD:getSystemPar( "teamReadyTime", 30 ) ) -- 战备倒计时
	self:_setCDTimeStr()
	if 0 < self._cdTime then
		if self.tickNumId then
			self:stopAction(self.tickNumId)
			self.tickNumId = nil
		end
		self.tickNumId = self:schedule(function()
			self._cdTime = self._cdTime - 1
			if 0 >= self._cdTime then
				self:stopAction(self.tickNumId)
				self.tickNumId = nil
				executeFunc(self._onCloseView)
			end
			self:_setCDTimeStr()
		end,1)
	end
end
function ClassRef:_setCDTimeStr()
	if 0 >= self._cdTime then
		return
	end
	self._tips:setString( string.format( L("bufftime_01"), self._cdTime ) )
end

-- 刷新队伍目标
function ClassRef:refreshTeamTargetStr( teamTarget )
	if nil == teamTarget or "" == teamTarget then
		self._teamTarget:setString( L("lua_code_text_55") )
	else
		self._teamTarget:setString( teamTarget )
	end
end

-- 刷新窗口title
function ClassRef:refreshTitle( title )
	self._teamReadyTitle:setString( title )
end

-- 刷新队伍战斗力
function ClassRef:refreshTeamCombatPower( power )
	self._teamMemberCombatPower:setString( power )
    display.setNodesMiddle(self._panelBom2, self._compatPowerImg, self._compatPowerTextTlt, self._teamMemberCombatPower)
end

-- 刷新队伍成员
function ClassRef:refreshTeamMember( maxNums, newTeamVo )
	self._teamMemberInfo:setData( newTeamVo )
	self._teamMemberInfo:refreshForbiddenIconStatus( maxNums )
	self._teamMemberInfo:refreshStopIconVisible()
end

-- 刷新队长随从信息
function ClassRef:refreshPartnerInfo( maxNums, partnerInfo )
	self._teamMemberInfo:refreshPartnerInfo( maxNums, partnerInfo, "yes" )
	self._teamMemberInfo:refreshForbiddenIconStatus( maxNums )
	self._teamMemberInfo:refreshStopIconVisible()
end

-- 刷新队伍成员准备状态
function ClassRef:refreshTeamMemberReadyStatus(isLegion, memberNum, robotNum)
	local renders = self._teamMemberInfo:getMemberRendres()
	for i,render in ipairs(renders) do
		if i <= memberNum then
			local member = render:getData()
			if member:isLeader() or member:isWithTeam() or member:isReady() then
				render:setReadyStatus( "yes" )
			else
				render:setReadyStatus( "none" )
			end
		elseif i <= (memberNum + robotNum) then
			-- 机器人都是准备好的
			render:setReadyStatus("yes")
		end
	end
end

-- 刷新队伍设置信息
function ClassRef:refreshTeamSetting( newTeamVo )
	self._teamSettingInfo:setData( newTeamVo )
end

-- 刷新战备UI按钮功能名字
function ClassRef:refresReadyUIBtnFunctionName( btnName )
	self._readySureBtn:getChildByName( "name" ):setString( btnName )
end

function ClassRef:onOperate(cb)
	self._onOperate = cb
end

return ClassRef
